package com.yanqu.road.dao.impl.activity.harps;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.harps.HarpsRewardInfo;
import com.yanqu.road.entity.activity.harps.HarpsUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HarpsUserDaoImpl extends TempDataObjectDao<HarpsUserData> {

    public static String getHarpsUserDataSql = "select * from `t_u_activity_harps_data` where `activityId`=? and `userId`=?";
    public HarpsUserData getHarpsUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHarpsUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHarpsUserDataSql = "update `t_u_activity_harps_data` set floor=?,perDrawReduceCount=?,round=?,playNum=?,playSum=?,score=?,scoreUpdateTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(HarpsUserData harpsUserData) {
        harpsUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPerDrawReduceCount()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPlayNum()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPlaySum()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsUserData.getScoreUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsUserData.getUserId()));
        if (getDb().executeNoneQuery(updateHarpsUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHarpsUserDataSql = "insert into `t_u_activity_harps_data`(activityId,userId,floor,perDrawReduceCount,round,playNum,playSum,score,scoreUpdateTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HarpsUserData harpsUserData) {
        harpsUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPerDrawReduceCount()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPlayNum()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getPlaySum()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsUserData.getScoreUpdateTime()));
        if (getDb().executeNoneQuery(insertHarpsUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HarpsUserData parseData(ResultSet rs) throws SQLException {
        HarpsUserData harpsUserData = new HarpsUserData();
        harpsUserData.setActivityId(rs.getInt("activityId"));
        harpsUserData.setUserId(rs.getLong("userId"));
        harpsUserData.setFloor(rs.getInt("floor"));
        harpsUserData.setPerDrawReduceCount(rs.getInt("perDrawReduceCount"));
        harpsUserData.setRound(rs.getInt("round"));
        harpsUserData.setPlayNum(rs.getInt("playNum"));
        harpsUserData.setPlaySum(rs.getInt("playSum"));
        harpsUserData.setScore(rs.getInt("score"));
        harpsUserData.setScoreUpdateTime(rs.getLong("scoreUpdateTime"));

        return harpsUserData;
    }
}
